package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttn.business.service.CourseUnitHearingService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitHearing;
import com.ttn.common.core.entity.business.bo.CourseUnitHearingBO;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitHearingSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * (course_unit_hearing)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-hearing")
@Slf4j
public class CourseUnitHearingController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitHearingService courseUnitHearingService;

    /**
     * 保存听力
     *
     * @param request
     * @param courseUnitHearingBO
     * @return
     */
    @PostMapping("/save/course-unit-hearing")
    public Result saveCourseUnitHearing(HttpServletRequest request, @RequestBody CourseUnitHearingBO courseUnitHearingBO) {
        log.info("CourseUnitHearingController saveCourseUnitHearing ,参数:{}", courseUnitHearingBO);
        return courseUnitHearingService.saveCourseUnitHearing(courseUnitHearingBO);
    }

    /**
     * 修改听力
     *
     * @param request
     * @param courseUnitHearingBO
     * @return
     */
    @PutMapping("/modify/course-unit-hearing")
    public Result modifyCourseUnitHearing(HttpServletRequest request, @RequestBody CourseUnitHearingBO courseUnitHearingBO) {
        log.info("CourseUnitHearingController saveCourseUnitHearing ,参数:{}", courseUnitHearingBO);
        return courseUnitHearingService.modifyCourseUnitHearing(courseUnitHearingBO);
    }

    /**
     * 删除听力
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("/delete/course-unit-hearing-by-id/{id}")
    public Result deleteCourseUnitHearingById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitHearingController deleteCourseUnitHearing ,参数id:{}", id);
        return courseUnitHearingService.deleteCourseUnitHearingById(id);
    }

    /**
     * 通过id查看听力详情
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping("/query/course-unit-hearing-by-id/{id}")
    public Result queryCourseUnitHearingById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitHearingController queryCourseUnitHearingById ,参数id:{}", id);
        return courseUnitHearingService.queryCourseUnitHearingById(id);
    }

    /**
     * 通过单元id,查询单元下面的听力分页
     *
     * @param request
     * @param courseUnitId
     * @param pageQuery
     * @return
     */
    @PostMapping("/query/course-unit-hearing-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitHearingByCourseUnitIdPage(HttpServletRequest request,
                                                           @PathVariable("courseUnitId") Integer courseUnitId,
                                                           @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitHearingController queryCourseUnitHearingById ,参数courseUnitId:{},pageQuery:{}",
                courseUnitId, pageQuery);
        return courseUnitHearingService.queryCourseUnitHearingByCourseUnitIdPage(courseUnitId, pageQuery);
    }

    /**
     * 修改听力排序
     *
     * @param request
     * @param modifyCourseUnitHearingSort
     * @return
     */

    @PutMapping("/modify/course-unit-hearing-sort")
    public Result modifyCourseUnitHearingSort(HttpServletRequest request, @RequestBody ModifyCourseUnitHearingSort modifyCourseUnitHearingSort) {
        log.info("CourseUnitHearingController modifyCourseUnitHearingSort ,参数modifyCourseUnitHearingSort:{}", modifyCourseUnitHearingSort);
        CourseUnitHearing courseUnitHearing = courseUnitHearingService.getById(modifyCourseUnitHearingSort.getId());
        LambdaQueryWrapper<CourseUnitHearing> courseUnitHearingLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitHearingLambdaQueryWrapper.ne(CourseUnitHearing::getId, modifyCourseUnitHearingSort.getId());
        courseUnitHearingLambdaQueryWrapper.eq(CourseUnitHearing::getCourseUnitId, courseUnitHearing.getCourseUnitId());
        courseUnitHearingLambdaQueryWrapper.orderByAsc(CourseUnitHearing::getSort);
        List<CourseUnitHearing> courseUnitHearingList = courseUnitHearingService.list(courseUnitHearingLambdaQueryWrapper);
        if (courseUnitHearingList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitHearingSort.getSortingBehavior().equals(-1)) {
                if (courseUnitHearing.getSort() - 2 > 0) {
                    sortIndex = courseUnitHearing.getSort() - 2;
                }
            } else if (modifyCourseUnitHearingSort.getSortingBehavior().equals(1)) {
                sortIndex = courseUnitHearing.getSort();
            }
            courseUnitHearingList.add(sortIndex, courseUnitHearing);
            AtomicInteger i = new AtomicInteger(1);
            courseUnitHearingList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitHearingService.updateBatchById(courseUnitHearingList);
        }
        return Result.success();
    }
}
